import unittest
from time import sleep

from parameterized import parameterized
from selenium.webdriver.common.by import By

from base import base
from tools.w_read_data import read_data
from page.w_warehouse_manage_page import WarehousePage


def get_data1():
    datas1 = read_data('../data/w_warehourse_manage1.json')
    print(datas1)
    arr = []

    for data in datas1.values():
        arr.append((data['name'], data['nickname'], data['weight'], data['contact'], data['phone'], data['address'],
                    data['expect']))
    print(arr)
    return arr


def get_data2():
    datas2 = read_data('../data/w_warehourse_manage2.json')
    arr = []
    for data in datas2.values():
        arr.append((data['expect']))
    return arr


def get_data3():
    datas3 = read_data('../data/w_warehourse_manage3.json')
    arr = []
    for data in datas3.values():
        arr.append((data['name'], data['nickname'], data['weight'], data['contact'], data['phone'], data['address'],
                    data['expect']))
    return arr


class TestWarehouse(unittest.TestCase):
    warehouse = None

    @classmethod
    def setUpClass(cls) -> None:
        cls.warehouse = WarehousePage()
        cls.warehouse.login_admin()

    @classmethod
    def tearDownClass(cls) -> None:
        sleep(3)
        cls.warehouse.driver.quit()

    @parameterized.expand(get_data1())
    def test_add_warehouse(self, name, nickname, weight, contact, phone, address, expect):
        msg = self.warehouse.add_warehouse(name, nickname, weight, contact, phone, address)
        print('预期结果为：', expect, '实际结果为：', msg)
        try:
            self.assertEqual(msg, str(expect))
        except:
            self.warehouse.get_image('w')
            raise

    @parameterized.expand(get_data2())
    def test_delete_warehouse(self, expect):
        msg = self.warehouse.delete_warehouse()
        print('预期结果为：', expect, '实际结果为：', msg)
        try:
            self.assertEqual(msg, str(expect))
        except:
            self.warehouse.get_image('w')
            raise

    @parameterized.expand([('zs', '10', '90', 'ls', '12345678901', "1111")])
    def test_reset_search_warehouse(self, name, min, max, nickname, phone, address):
        self.warehouse.reset_search_warehouse(name, min, max, nickname, phone, address)

    @parameterized.expand(get_data3())
    def test_sidebar_warehouse(self, name, nickname, weight, contact, phone, address, expect):
        msg = self.warehouse.sidebar_warehouse(name, nickname, weight, contact, phone, address)
        print('预期结果为:', expect, '实际结果为:', msg)
        try:
            self.assertEqual(msg, str(expect))
        except:
            self.warehouse.get_image('w')
            raise
